package com.wwwarehouse.resource_center.fragment.resource_efficiency_evaluation;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.fragment.CommonBridgeWebViewFragment;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.WebDataBean;

/**
 * @author Joseph on 2017/11/1.
 *         <p/>
 *         资源效率webView
 */

public class ResourceEfficiencyWebViewFragment extends CommonBridgeWebViewFragment {
    private int flag = 1;
    private boolean pop = true;

    @Override
    public void requestDatas() {
        super.requestDatas();
        Log.d("getBusinessEfficiency", mData);
        mWebView.callHandler("getBusinessParam", mData, new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                ToastUtils.showToast(data);
            }
        });
        mWebView.registerHandler("didClickTrendChartAction", new BridgeHandler() {
            public void handler(String data, CallBackFunction function) {
                if (!TextUtils.isEmpty(data)) {
                    flag += 1;
                    WebDataBean dataBean = JSON.parseObject(data, WebDataBean.class);
                    WebDataBean dataBean1 = JSON.parseObject(mData, WebDataBean.class);
                    dataBean1.setIndex(dataBean.getIndex());
                    Bundle bundle = new Bundle();
                    bundle.putString("title", getString(R.string.trend_chart_webview));
                    bundle.putString("data", JSON.toJSONString(dataBean1));
                    TrendChartWebViewFragment trendChartWebViewFragment = new TrendChartWebViewFragment();
                    trendChartWebViewFragment.setArguments(bundle);
                    pushFragment(trendChartWebViewFragment);
                }
            }
        });
        mWebView.registerHandler("showCalculationFormulaAlert", new BridgeHandler() {
            public void handler(String data, CallBackFunction function) {
                DialogTools.getInstance().showCustomWarning(mActivity, "计算公式", "1.资源收益率=（当期资源总收入-当期总成本）/" +
                        "持有平均资产总额*100%\n" + "2.周转率=（当期销售数量/当前库存平均数量）*100%", "确定", true, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String s) {
                        dialog.dismiss();
                    }
                });
            }
        });
        mWebView.registerHandler("didClickFullScreenButtonAction", new BridgeHandler() {
            public void handler(String data, CallBackFunction function) {
                WebDataBean dataBean = JSON.parseObject(data, WebDataBean.class);
                WebDataBean dataBean1 = JSON.parseObject(mData, WebDataBean.class);
                dataBean1.setIndex(dataBean.getIndex());
                dataBean1.setUrl(dataBean.getUrl());
                Intent intent = new Intent(getActivity(), ResourceWebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("data", dataBean1);
                intent.putExtras(bundle);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ResourceEfficiencyWebViewFragment)
            mActivity.setTitle(R.string.res_evaluation);
    }

    @Override
    public void onEventMainThread(BackListenerEvent event) {
        if (pop) {
            pop = false;
            if (flag == 2) {
                pop = true;
            }
            flag -= 1;
            popFragment();
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//            super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
//                    String index = data.getStringExtra("data");
                Log.d("onActivityResult", "index");
                break;
        }
    }
}
